<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
/**
 * 前台默认控制器
 * @author jry <598821125@qq.com>
 */
class IndexController extends HomeController{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
     //首頁
    public function index(){
        $banner=M('Banner')->order("sort asc")->select();//Banner
        $this->assign("banner", $banner);
        
        $about=M('About')->find(1);     //關於我們
         $this->assign('about',$about);
         
         $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
        
        $data['recommend']=1;
        $clothes=M('Clothes')->where($data)->order("sort asc")->select();
        $this->assign("clothes",$clothes);
        $this->assign('meta_title', "首页");
        $this->display('');
    }
    //時裝頁
     public function products(){
          $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
          
        $banner=M('Banner')->order("sort asc")->select();//Banner
        $this->assign("banner", $banner);
        $type=I('type');
//        if($type==0){
//             $data['new']=1;
//             $this->assign('title', "新品");
//        }elseif($type==1){
//             $data['type']=1;
//             $this->assign('title', "男裝");
//        }elseif($type==2){
//             $data['type']=2;
//             $this->assign('title', "女裝");
//        }elseif($type==3){
//             $data['type']=3;
//             $this->assign('title', "兒童裝");
//        }elseif($type==4){
//            $data['discount']=array('gt',0);
//            $this->assign('title', "活動優惠");
//        }
        $d=M('Clothes');
        if($type=="new"){
            $data['new']=1;
            $this->assign('title', "新品");
        }elseif($type=="youhui"){
            $data['discount']=array('gt',0);
            $this->assign('title', "活動優惠");
        }else{
            $data['type']=$type;
            $info['id']=$type;
            $title=M('Type')->where($info)->find();
            $this->assign('title', $title['title']);
        }
        
        $about=M('About')->find(1);     //關於我們
         $this->assign('about',$about);
        
        
        $count  =$d->where($data)->count();
        $Page   =new \Think\Page($count,8);
        $show   = $Page->show();
        $clothes=$d->where($data)->order("sort asc")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign("type",$type);
        $this->assign("clothes",$clothes);
        $this->assign('page',$show);
        $this->assign('meta_title', "時裝");
        $this->display('');
    }
    //商品詳細頁
    public function detail(){
        $id=I('id');
        $clothes=M('clothes')->find($id);
        $re['type']=$clothes['type'];
        $typeinfo['id']=$clothes['type'];
        $type=M("Type")->where($typeinfo)->find();
        $this->assign('type',$type);
         
        $color= explode(",",$clothes['color']);//顏色
        $this->assign("color",$color);
         
        $img= explode(",",$clothes['img']);//圖片
        foreach($img as $file){
            
            $f[$file] = D('PublicUpload')->find($file);
        }
        $this->assign("info_img",$f);
//         $colorimg=array();
//         foreach($color as $k=>$v){
//              $colorimg[$k]['color']=$v;
//              $colorimg[$k]['img']=$img[$k];
//         }
//         $this->assign("colorimg",$colorimg);
         
         $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
         
         $size=explode(",",$clothes['size']);//尺寸
         $this->assign("size",$size);
         
         $recommend=M('clothes')->where($re)->order("rand()")->limit(3)->select();//同類推薦
         $this->assign("recommend",$recommend);
         
         $about=M('About')->find(1);     //關於我們
         $this->assign('about',$about);
         
         $this->assign("clothes",$clothes);
         $this->assign('meta_title', "時裝詳細");
        $this->display('');
    }
    //ajax同類推薦
    public function recommend(){
         $type['type']=I("type");
         $recommend=M('clothes')->where($type)->order("rand()")->limit(3)->select();//同類推薦
         foreach($recommend as $k=>$v){
//              echo "<div class='simpli-li col-md-12 col-sm-6 col-xs-6 p-0'>"
//              . "<div class='h-100 c-e-i c-h-i'><a href='Home/Index/detail/id/".$v[id]."'><img src='".get_cover($v['img'])."' height='200' width='168'></a></div>"
//                      . "<p>".$v['name']."</p>
//                        <p>$".$v['price']."</p>
//                    </div>";
            $re['id'][]=$v['id'];
            $re['name'][]=$v['name'];
            $re['img'][]=  get_cover($v['img']);
            $re['price'][]=$v['price'];
            if($v['discount']<=0){
                $re['discount'][]="無";
            }else{
                $re['discount'][]="$"+$v['discount'];
            }
            
         }
         $this->ajaxReturn($re);
    }
    //加入購物車
    public function addcart(){
         session_start();
         if($_SESSION['islogin']!=1){
              $this->ajaxReturn("請先登錄！");
         }
         $id=I("id");
         $name=I("name");
         $color=I("color");
         
         $good=M("Clothes")->find($id);
         if($good['stock']<=0){
              $this->ajaxReturn("該商品無貨！");
         }
         if($color=="請選擇"){
              $this->ajaxReturn("請選擇顏色！");
         }
         $size=I("size");
         $price=I("price");
         $img=I("img");
         if($_SESSION['cart'][$id][$id]=$id){
               $_SESSION['cart'][$id]["name"]=$name;
               $_SESSION['cart'][$id]["color"]=$color;
               $_SESSION['cart'][$id]["size"]=$size;
               $_SESSION['cart'][$id]["price"]=$price;
               $_SESSION['cart'][$id]["img"]=$img;
               $this->ajaxReturn("加入購物車成功！");
         }else{
              $this->ajaxReturn("加入購物車失敗！");
         }
        
    }
    //刪除購物車
    public function delcart(){
         session_start();
         $id=I('id');
         unset($_SESSION['cart'][$id]);
        
         $this->ajaxReturn("刪除購物車成功！");
    }
    
    //立即購買
    public function addone(){
         session_start();
        if($_SESSION['islogin']!=1){
            $this->ajaxReturn("請先登錄！");
         }
         $uid=$_SESSION['id'];
         $user=M('Member')->find($uid);
         $id=I('id');
         $name=I('name');
         $color=I('color');
         $size=I('size');
         $price=I('price');
         $num=I('num');
         $img=I('img');
         $clothes=M('Clothes')->find($id);
         if($clothes['stock']<=0){
              $this->ajaxReturn($clothes['name']."無貨");
         }
         if($clothes['stock']-$num<0){
              $this->ajaxReturn($clothes['name']."超出最大庫存,請檢查購買數量");
         }
         if($color=="請選擇"){
              $this->ajaxReturn("請選擇顏色");
         }
         $data=array(
             'contact'  =>$user['name'],
             'address'  =>$user['address'],
             'tel'          =>$user['tel'],
             'email'     =>$user['email'],
             'name'     =>$name,
             'color'      =>$color,
             'size'        =>$size,
             'num'       =>$num,
             'price'      =>$price,
             'img'        =>$img,     
             'allprice'  =>$num*$price,
         );
         $re['id']=$clothes['id'];
         $re['stock']=$clothes['stock']-$num;
        
         $info_email=M('About')->find(1);              //獲取郵箱
         
              $result= M("Order")->add($data);
              if($result){
                   $d=M('Clothes')->save($re);
                   $mail_data['receiver'] =$info_email['email'];
                   $mail_data['subject']  = '利詩訂單';
                   $mail_data['content'] = "<table border='1' cellpadding='0' cellspacing='0'><tr><td>聯繫人：</td><td>".$user['name']."</td></tr>"
                                 . "<tr><td>郵箱：</td><td>".$user['email']."</td></tr>"
                                 . "<tr><td>電話：</td><td>".$user['tel']."</td></tr>"
                                 . "<tr><td>聯繫地址：</td><td>".$user['address']."</td></tr>"
                                 . "<tr><td>商品名稱：</td><td>".$name."</td></tr>"
                                 . "<tr><td>商品顏色：</td><td>".$color."</td></tr>"
                                 . "<tr><td>商品尺寸：</td><td>".$size."</td></tr>"
                                 . "<tr><td>商品單價：</td><td>".$price."</td></tr>"
                                 . "<tr><td>商品數量：</td><td>".$num."</td></tr>"
                                 . "<tr><td>商品總價：</td><td>".$num*$price."</td></tr></table>";
                   //发送邮件
                         if(send_mail($mail_data)){
                             $this->ajaxReturn('下單成功，祝你購物愉快！');
                         }else{
                             $this->ajaxReturn('发送失败！');
                         }
              }else{
                   $this->ajaxReturn("下單失敗，請再執行一次！");
              }
         
    }
    
    //提交訂單
    public function addorder(){
        session_start();
        if($_SESSION['islogin']!=1){
            $this->error("你还没登录");
         }
         
         $order_data=array();
         $i=0;
         $id=I('id');
         if($id==null){
               $this->error('購物車沒商品');
         }
         $uid=$_SESSION['id'];
         $user=M('Member')->find($uid);
         $str_id=implode(',',$id);
         $name=I('name');
         $color=I('color');
         $size=I('size');
         $price=I('price');
         $num=I('num');
         $data=M("Clothes")->where("id in ($str_id)")->select();
         
         $info_email=M('About')->find(1);       //獲取郵箱
         
         $mail_data['receiver'] =$info_email['email'];
         $mail_data['subject']  = '利詩訂單';
         $mail_data['content']="";
         foreach($data as $k=>$v){
              if($v['stock']<=0){
                   $this->error($v['name']."無貨");
              }
              $order_data['contact']=$user['name'];
              $order_data['address']=$user['address'];
              $order_data['tel']=$user['tel'];
              $order_data['email']=$user['email'];
              $order_data['name']=$v['name'];
              $order_data['color']=$color[$i];
              $order_data['size']=$size[$i];
              $order_data['price']=$price[$i];
              $order_data['img']=$v['img'];
              $order_data['num']=$num[$i];
              $order_data['allprice']=$num[$i]*$price[$i];
              $order_data['status']=0;
              
              $mail_data['content'] .=(string) "<table border='1' cellpadding='0' cellspacing='0'><tr><td>聯繫人：</td><td>".$user['name']."</td></tr>"
                                 . "<tr><td>郵箱：</td><td>".$user['email']."</td></tr>"
                                 . "<tr><td>電話：</td><td>".$user['tel']."</td></tr>"
                                 . "<tr><td>聯繫地址：</td><td>".$user['address']."</td></tr>"
                                 . "<tr><td>商品名稱：</td><td>".$v['name']."</td></tr>"
                                 . "<tr><td>商品顏色：</td><td>".$color[$i]."</td></tr>"
                                 . "<tr><td>商品尺寸：</td><td>".$size[$i]."</td></tr>"
                                 . "<tr><td>商品單價：</td><td>".$price[$i]."</td></tr>"
                                 . "<tr><td>商品數量：</td><td>".$num[$i]."</td></tr>"
                                 . "<tr><td>商品總價：</td><td>".$num[$i]*$price[$i]."</td></tr></table><br>";
              
              $order=M('Order')->add($order_data);
              if($v['stock']-$num[$i]<0){
                   $this->error($v['name']."超出最大庫存,請檢查購買數量");
              }
              $clothes_stock['stock']=$v['stock']-$num[$i];
              $clothes_stock['id']=$v['id'];
              $c_stock=M('Clothes')->save($clothes_stock);
              $i++;
         }
           if($order){
                
                $_SESSION['cart']=null;
                    
                     //发送邮件
                         if(send_mail($mail_data)){
                             $this->success("下單成功，祝你購物愉快！");
                         }else{
                             $this->error('发送失败！');
                         }
            }else{
                $this->error("下單失敗，請再執行一次！");
            }
         
    }
    //最新動態
    public function latest(){
         $d=M('Latest');
         $count      = $d->count();
         $Page       = new \Think\Page($count,4);
         $show       = $Page->show();
         $latest=$d->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
         $about=M('About')->find(1);
         
         $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
         
         $this->assign('about',$about);
         $this->assign('page',$show);
         $this->assign('latest',$latest);
         $this->assign('meta_title', "最新動態");
        $this->display('');
    }
    //最新动态详细页
     public function latestpage(){
          $id=I('id');
         $latest=M('Latest')->find($id);
         $about=M('About')->find(1);
         
         $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
         
         $this->assign('about',$about);
         $this->assign('latest',$latest);
         $this->assign('meta_title', "最新動態");
        $this->display('');
    }
    //登录
    public function login(){
        if(IS_POST){
            $username = I('name');
            $password = I('pwd');
            if(!$username){
                $this->ajaxReturn('请输入账号！');
            }
            if(!$password){
                $this->ajaxReturn('请输入密码！');
            }
            $user_object = D('Member');
            $uid = $user_object->login($username, $password);
            if(0 < $uid){
                $this->ajaxReturn('登錄成功！');
            }else{
                $this->ajaxReturn($user_object->getError());
            }
        }else{
            if(is_login_m()){
                $this->ajaxReturn("您已登陆系统", Cookie('__forward__') ? : C('HOME_PAGE'));
            }
//            $this->meta_title = '用户登录';
//            $this->display();
        }
    }
   
    
    //注销退出
    public function logout(){
         session_start();
         session('member_auth', null);
        session('member_auth_sign', null);
        unset($_SESSION['islogin']);
        unset($_SESSION['username']);
        $this->ajaxReturn('退出成功！');
    }
    
    //注册
    public function registered(){
         $name=I("name");
         $pwd=I("pwd");
         $repwd=I("repwd");
         $email=I("email");
         $address=I("address");
         $tel=I("tel");
         
          if(!$name){
                $this->ajaxReturn('请输入會員名！');
            }
            if(!$pwd){
                $this->ajaxReturn('请输入密碼！');
            }
            if(!$repwd){
                $this->ajaxReturn('请输入確認密碼！');
            }
            if($repwd!=$pwd){
                $this->ajaxReturn('兩個密碼不一致！');
            }
            if(!$email && !$tel){
                $this->ajaxReturn('请输入手機號碼或者郵箱！');
            }
             if(!$address){
                $this->ajaxReturn('请输入聯繫地址！');
            }
            
            
            
            
            $data['name']=$name;
            $user=M("Member")->where($data)->count();
            if($user>0){
                 $this->ajaxReturn("該用戶已存在！");
            }
            $data['pwd']=  user_md5($pwd);
            $data['repwd']=$repwd;
            $data['email']=$email;
            $data['tel']=$tel;
            $data['address']=$address;
            $result=D('Member')->add($data);
            if($result){
                 $this->ajaxReturn("註冊成功！");
            }else{
                 $this->ajaxReturn("註冊失敗！");
            }
    }
    
    
    //搜索
    public function search(){
         $keyword=$keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
         $map['name|color|size|info|describe'] = array($condition, $condition, $condition,$condition,$condition,'_multi'=>true);
         $info=M('Clothes');
        $count  =$info->where($map)->count();
        $Page   =new \Think\Page($count,12);
        $show   = $Page->show();
        $clothes=$info->where($map)->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        
        $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
        
        $about=M('About')->find(1);     //關於我們
         $this->assign('about',$about);
        $this->assign("clothes",$clothes);
        $this->assign("page",$show);
        $this->assign('title', "搜索");
        $this->assign('meta_title', "搜索");
        $this->display();
    }
    
    //高級定制
    public function customize(){
         session_start();
         if($_SESSION['islogin']!=1){
            $this->error("你还没登录");
         }
         $uid=$_SESSION['id'];
         $user=M('Member')->find($uid);
        $con['md5']  = md5_file($_FILES['file']['tmp_name']);
        $con['sha1'] = sha1_file($_FILES['file']['tmp_name']);
        $con['size'] = $_FILES['file']['size'];
        $name=I('name');
        $describe=I('describe');
        $color=I('color');
        $size=I('size');
        
            $return['name']=$name;
            $return['describe']=$describe;
            $return['color']=$color;
            $return['size']=$size;
            $return['contact']=$user['name'];
            $return['address']=$user['address'];
            $return['tel']=$user['tel'];
            
            $info_email=M('About')->find(1);       //獲取郵箱
            
             $mail_data['receiver'] =$info_email['email'];
             $mail_data['subject']  = '利詩定制';
             $mail_data['content'] = "<table border='1' cellpadding='0' cellspacing='0'><tr><td>聯繫人：</td><td>".$user['name']."</td></tr>"
                                 . "<tr><td>郵箱：</td><td>".$user['email']."</td></tr>"
                                 . "<tr><td>電話：</td><td>".$user['tel']."</td></tr>"
                                 . "<tr><td>聯繫地址：</td><td>".$user['address']."</td></tr>"
                                 . "<tr><td>商品名稱：</td><td>".$name."</td></tr>"
                                 . "<tr><td>商品描述：</td><td>".$describe."</td></tr>"
                                 . "<tr><td>商品顏色：</td><td>".$color."</td></tr>"
                                 . "<tr><td>商品尺寸：</td><td>".$size."</td></tr></table>";
        
        $upload = M('PublicUpload')->where($con)->find();
        if($upload){ //发现相同文件直接返回
            $return['img']   = $upload['id'];
            $result=M('Customize')->add($return);
            if($result){
                        

                         //发送邮件
                         if(send_mail($mail_data)){
                             $this->success('定制成功，已通知到商家！');
                         }else{
                             $this->error('发送失败！');
                         }
            }else{
                 $this->error("定制失敗");
            }
            
        }else{
               $upload = new \Think\Upload();// 实例化上传类
               $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型

               $info   =   $upload->upload();
               if(!$info) {// 上传错误提示错误信息
                    $result=M('Customize')->add($return);
                    if($result){
                         
                        
                         //发送邮件
                         if(send_mail($mail_data)){
                             $this->success('定制成功，已通知到商家！');
                         }else{
                             $this->error('发送失败！');
                         }
                         
                    }else{
                         $this->error("定制失敗");
                    }
               }else{// 上传成功        
                    $info = $info['file'];
                    $upload_data['type'] = $info["type"];
                    $upload_data['name'] = $info["name"];
                    $upload_data['path'] = '/Uploads/' . $info['savepath'] . $info['savename'];
                    $upload_data['url'] = $info["url"] ? : '';
                    $upload_data['ext'] = $info["ext"];
                    $upload_data['size'] = $info["size"];
                    $upload_data['md5']  = $info['md5'];
                    $upload_data['sha1']  = $info['sha1'];
                    $upload_data['ctime']  = time();
                    $upload_data['utime']  = time();
                    $upload_data['status']  = 1;
                    $result = M('PublicUpload')->create($upload_data);
                    $result = M('PublicUpload')->add($result);
                    
                    $upload1 = M('PublicUpload')->where($con)->find();
                     $return['img']   = $upload1['id'];
                    $result=M('Customize')->add($return);
                    if($result){
                         
                        
                         //发送邮件
                         if(send_mail($mail_data)){
                             $this->success('定制成功，已通知到商家！');
                         }else{
                             $this->error('发送失败！');
                         }
                         
                    }else{
                         $this->error("定制失敗");
                    }
                    
               }
        }
    }
    
    
    //關於利詩
    public function about(){
         
         $logo=M('Logo')->find(1);      //LOGO
         $this->assign('logo',$logo);
         $about=M('About')->find(1);
         $this->assign("about",$about);
         $this->assign('meta_title', "關於利詩");
         $this->display();
    }
    
    
    //服務支持
    public function service(){
        $logo=M('Logo')->find(1);      //LOGO
        $this->assign('logo',$logo);
        
        $about=M('About')->find(1);     //關於我們
        $this->assign('about',$about);
        
        $service=M("Service")->find(1);
        $this->assign("service",$service);
        $this->assign('meta_title', "服務支持");
        $this->display();
    }
    //買賣須知
    public function support(){
        $logo=M('Logo')->find(1);      //LOGO
        $this->assign('logo',$logo);
        
        $about=M('About')->find(1);     //關於我們
        $this->assign('about',$about);
        
        $service=M("Service")->find(2);
        $this->assign("service",$service);
        $this->assign('meta_title', "買賣須知");
        $this->display();
    }


}
